package com.itfreer.forestry.manage.token;

import com.itfreer.forestry.manage.token.common.MinglingOutPut;
import com.itfreer.forestry.manage.utils.RSAUtils;
import com.itfreer.utils.json.JsonUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by 2020/11/29
 *
 * @author yangyang
 */
public class DockTokenInter {

    public static void initToken(){

        getPublicKey();
        userlogin();
    }

    /**
     * 获取公钥
     * @return publicKey
     * */
    public static String getPublicKey() {


        try{
            HttpClient httpclient = HttpClientBuilder.create().build();
            URI uri=new URI(MinglingOutPut.getPublicKeyUrl());
            HttpGet httpGet = new HttpGet(uri);
            HttpResponse result = httpclient.execute(httpGet);
            if (result.getStatusLine().getStatusCode() == 200) {
                String str = "";
                // 读取服务器返回过来的json字符串数据
                str = EntityUtils.toString(result.getEntity());

                // 把json字符串转换成json对象，方便操作
                String jsonResult = JsonUtils.toJSONString(str);
                Map maps = (Map)JsonUtils.toBean(str,Map.class);
                Map map = (Map)maps.get("data");
                String publicKey = (String) map.get("publicKey");
                MinglingOutPut.setPublicKey(publicKey);
                //查询返回的参数是List<List<String>>格式
            }else {
                System.out.println("请求失败");
            }
        } catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

    /***
     * 用户登录，返回token
     * @return token
     * */
    public static String userlogin(){


        // 拼接 授权码
        String ciphertext = MinglingOutPut.AppKey + "#" + MinglingOutPut.AppSecret;

        try {

            ciphertext = RSAUtils.encrypt(ciphertext, MinglingOutPut.publicKey);
            //HTTP连接
            HttpClient httpclient = HttpClientBuilder.create().build();
            //请求地址
            URI uri=new URI(MinglingOutPut.getTokenUrl());
            //post请求
            HttpPost post = new HttpPost(uri);

            //请求参数
            List<NameValuePair> params = new ArrayList<>();
            NameValuePair nvciphertext=new BasicNameValuePair("ciphertext", ciphertext);
            NameValuePair nvcGrantType=new BasicNameValuePair("grantType", "client_credentials");
            params.add(nvcGrantType);
            params.add(nvciphertext);
            post.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));

            //请求头
            post.setHeader("Content-Type","application/x-www-form-urlencoded");
            //访问
            HttpResponse result = httpclient.execute(post);
            //返回结果
            if (result.getStatusLine().getStatusCode() == 200) {
                String str = "";
                // 读取服务器返回过来的json字符串数据
                str = EntityUtils.toString(result.getEntity());

                // 把json字符串转换成json对象，方便操作
                String jsonResult = JsonUtils.toJSONString(str);
                Map maps = JsonUtils.toBean(str,Map.class);
                Map map = (Map)maps.get("data");
                String token = (String) map.get("access_token");
                MinglingOutPut.setToken(token);
            }else {
                System.out.println("请求失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


}
